package com.hjq.fcloud.other

import android.os.Bundle
import android.text.TextUtils
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.config.SystemSet
import com.hjq.fcloud.http.api.InitAppApi
import com.hjq.fcloud.http.api.VerifyQRCodeValidityApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.jump.CommonJump
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.ui.activity.FaceConfirmGuidePageActivity
import com.hjq.fcloud.utils.Logger
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallback
import okhttp3.Call

object QrCodeParse {

    //人脸识别
    const val TYPE_FACE = "face_"

    val INTENT_IN_STEP_TOKEN = "stepToken"

    /**
     * 扫码解析
     */
    fun perforomQrCodeParse(context: AppActivity, showResult: String?) {
        try {
            Logger.i("showResult:" + showResult)
            if (!TextUtils.isEmpty(showResult)) {
                //showResult   FUC_100401_24_URL_  一个biz_code，一个biz_id
                if (showResult!!.startsWith(CommonJump.MAKRER_SPLIT)) {
                    var array = showResult?.split("_")

                    var jumpBean = CommonJumpBean()
                    var bizCode = CommonJump.MAKRER_SPLIT.plus(array?.get(1))
                    jumpBean.setBizCode(bizCode)
                    jumpBean.bizId = array?.get(2)
                    jumpBean.isStartSign = true
                    jumpBean.isFromScanQrCode = true
                    CommonJump.commonJump(context, jumpBean)
                }else if(showResult!!.startsWith(TYPE_FACE)){
                    verifyQRCodeValidity(context,showResult)
                }
            } else {
                context.showToast("无效二维码")
            }
        } catch (e: Exception) {
            context.showToast("无效二维码")
        }
    }


    /**
     * 验证二维码有效性
     */
    private fun verifyQRCodeValidity(context: AppActivity,stepToken:String) {
        var api = VerifyQRCodeValidityApi()
            .setStep_token(stepToken)
        EasyHttp.post(context)
            .api(api)
            .request(object : HttpCallback<HttpData<VerifyQRCodeValidityApi.Result>?>(context) {
                override fun onSucceed(data: HttpData<VerifyQRCodeValidityApi.Result>?) {
                    var b = Bundle()
                    b.putString(INTENT_IN_STEP_TOKEN,stepToken)
                    FaceConfirmGuidePageActivity.launcher(context,b)
                }
            })
    }
}